package memleak.test.com.testlist;

import android.content.Context;
import android.content.SharedPreferences;


import com.gzgd.log.LogUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Kelly on 2017/3/14.
 */

public class PreferencesDataSource {

    private static final String TAG = "PreferencesDataSource";
    private static final String PREF_PAGE_POS = "pagePos";
    private static final String PREF_CHANNEL_POS = "channelPos";
    private static final String PREF_KEY_TOKEN = "PREF_KEY_TOKEN";
    private static final String PREF_KEY_TOKEN_REFRESH_TIME = "PERF_KEY_TOKEN_REFRESH_TIME";
    private static final String PREF_KEY_TABS = "PREF_KEY_TABS";
    private static final String PREF_KEY_VALID_TIME = "VALID_TIME";
    private static final String PREF_NAME = "Live_pref";
    private static final String PREF_KEY_LIVE_CHANNELCATEGORY = "PREF_KEY_LIVE_CHANNELCATEGORY";
    private static final String PREF_KEY_CHANNEL_NUM = "PREF_KEY_PRE_CHANNEL_NUM"; //频道号
    private static final String PREF_KEY_CHANNEL_CATEGORY = "PREF_KEY_CHANNEL_CATEGORY"; //频道分类属性
    private SharedPreferences mSharePref;
    private String mDefaultCategoryId;

    private static final class PreferencesDataSourceHolder {
        private static final PreferencesDataSource instance = new PreferencesDataSource();
    }

    public void init(Context context) {
        mSharePref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
       // mDefaultCategoryId = ((LiveApplication) context.getApplicationContext()).getConfig().getDefaultCategoryId();
        mDefaultCategoryId = "0" ;
    }

    public static PreferencesDataSource newInstance() {
        return PreferencesDataSourceHolder.instance;
    }

    public String getToken() {
        return mSharePref.getString(PREF_KEY_TOKEN, "");
    }

    public void setToken(String token) {
        mSharePref.edit().putString(PREF_KEY_TOKEN, token).apply();
    }

    public String getTokenRefreshTime() {
        return mSharePref.getString(PREF_KEY_TOKEN_REFRESH_TIME, "");
    }

    public void setTokenRefreshTime(String time) {
        mSharePref.edit().putString(PREF_KEY_TOKEN_REFRESH_TIME, time).apply();
    }

    public String getTabKey() {
        return mSharePref.getString(PREF_KEY_TABS, "");
    }

    public void setTabKey(String value) {
        mSharePref.edit().putString(PREF_KEY_TABS, value).apply();
    }

    public String getValidTime() {
        return mSharePref.getString(PREF_KEY_VALID_TIME, "");
    }

    public void setValidTime(String valid_time) {
        mSharePref.edit().putString(PREF_KEY_VALID_TIME, valid_time).apply();
    }

    public void saveLiveChannelCategorys(Set<String> set) {
        mSharePref.edit().putStringSet(PREF_KEY_LIVE_CHANNELCATEGORY, set).apply();
        LogUtils.i(TAG, ">>>>>>>>>>>>>>>save all channel category key to preference");
    }

    public HashSet<String> getLiveChannelCategorys() {
        return (HashSet<String>) mSharePref.getStringSet(PREF_KEY_LIVE_CHANNELCATEGORY, null);
    }

    public void savePreChannelNum(int cNo) {
        mSharePref.edit().putInt(PREF_KEY_CHANNEL_NUM, cNo).apply();
    }

    public int getPreChannelNum() {
        return mSharePref.getInt(PREF_KEY_CHANNEL_NUM, 999);
    }

    public String getPreChannelCategoryID() {
        return mSharePref.getString(PREF_KEY_CHANNEL_CATEGORY, mDefaultCategoryId);
    }

    public void savePreChannelCategoryID(String channelCategory) {
        mSharePref.edit().putString(PREF_KEY_CHANNEL_CATEGORY, channelCategory).apply();
    }


    public void savePagePos(int pagePos) {
        mSharePref.edit().putInt(PREF_PAGE_POS, pagePos).apply();
    }

    public int getPagePos() {
        return mSharePref.getInt(PREF_PAGE_POS, 0);
    }

    public int getChannelPos() {
        return mSharePref.getInt(PREF_CHANNEL_POS, 0);
    }

    public void saveChannelPos(int channelPos) {
        mSharePref.edit().putInt(PREF_CHANNEL_POS, channelPos).apply();
    }

}
